package com.fg.cloud.factory.controller.factory;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.alibaba.fastjson.JSONArray;
import com.fg.cloud.common.StatusCode;
import com.fg.cloud.common.quantity.ResultType;
import com.fg.cloud.framework.controller.BaseController;
import com.fg.cloud.manager.entry.factory.FactoryUserEntity;
import com.fg.cloud.manager.entry.factory.vo.FactoryVo;
import com.fg.cloud.manager.service.factory.FactoryService;

/**
  * Description:厂商基本信息
  * @author shaoqiping
  * @date 2017年8月3日上午9:14:11
*/
@Controller
@RequestMapping("/factory")
public class FactoryController extends BaseController {

	@Autowired
	private FactoryService factoryService;
	/**
	  * Description:厂商基本信息修改
	  * @param request
	  * @param response
	  * @param factoryEntity
	  * @author shaoqiping
	  * @date 2017年8月3日上午9:14:43
	*/
	@RequestMapping(value = "/update", method = RequestMethod.POST)
	@ResponseBody
	public void update(HttpServletRequest request, HttpServletResponse response, FactoryVo factoryEntity) {
		try {
			// 条件判断
			if (factoryEntity == null || factoryEntity.getId() == null || factoryEntity.getId() <= 0) {
				doResult(response, false, StatusCode.LACKVALUE, null, ResultType.SAVE_FAC_FAULT);
				return;
			}
			factoryEntity.setId(getCurrentFactoryUser().getFacId());
			setUpdateInfo(factoryEntity, request);
			String administrative=factoryEntity.getAdministrative();
			JSONArray json=JSONArray.parseArray(administrative);
			if(json.size()==3){
				factoryEntity.setProvince((String) json.getJSONObject(0).get("label"));
				factoryEntity.setCity((String) json.getJSONObject(1).get("label"));
				factoryEntity.setArea((String) json.getJSONObject(2).get("label"));
			}
			int temp = factoryService.update(factoryEntity);
			if (temp > 0) {
				doResult(response, true, StatusCode.SAVESUCCESS, null, ResultType.SAVE_SUCCESS);
			} else {
				doResult(response, false, StatusCode.SAVEERROR, null, ResultType.SAVE_FAULT);
			}
		} catch (Exception e) {
			e.printStackTrace();
			doResult(response, false, StatusCode.EXCEPTIONERROR, null, e.getMessage());
		}
	}
	
	/**
	  * Description:厂商基本信息显示
	  * @param request
	  * @param response
	  * @author shaoqiping
	  * @date 2017年8月3日上午9:15:09
	*/
	@RequestMapping("/facInfo")
	@ResponseBody
	public void facinfo(HttpServletRequest request, HttpServletResponse response) {
		try {
			FactoryUserEntity factoryUserEntity = getCurrentFactoryUser();
			FactoryVo factoryEntity = factoryService.find(factoryUserEntity.getFacId());
			doResult(response, true, StatusCode.FINDSUCCESS, factoryEntity, ResultType.FIND_SUCCESS);
		} catch (Exception e) {
			e.printStackTrace();
			doResult(response, false, StatusCode.EXCEPTIONERROR, null, e.getMessage());
		}
	}
}
